import sys
import json
import yaml
import logging

from arbre import run
from message import sendMessage

def load(filename):
  f=open(filename, 'r')
  if '.yaml' in filename:
    y=yaml.load(f)
  else:
    y=json.load(f)
  f.close()
  return y

if __name__=='__main__':
  logging.basicConfig(level=logging.ERROR)

  system=load('system.json')
  userlib=load(sys.argv[1])

  testName=sys.argv[2]
  test={"main": {"type": "method", "code": ["func", [testName, []]], "params": []}}

  result=run(system, userlib, test, debug=False)
  logging.info('final result: '+str(result))
  value=result[2]
  if value[0]=='literal' and value[1][0]=='message':
    message=value[1]
    sendMessage(system, userlib, message, debug=False)
